package com.shazhu.nongjt.service.impl;

import java.util.Date;
import java.util.List;

import com.shazhu.common.constant.NjtConstants;
import com.shazhu.common.core.domain.AjaxResult;
import com.shazhu.common.core.domain.entity.SysUser;
import com.shazhu.common.enums.YesOrNoEnum;
import com.shazhu.common.utils.DateUtils;
import com.shazhu.nongjt.domain.NjtUnitConfig;
import com.shazhu.nongjt.domain.NjtUserRegister;
import com.shazhu.nongjt.domain.vo.NjtLoginUser;
import com.shazhu.nongjt.service.INjtCommonService;
import com.shazhu.nongjt.service.INjtUnitConfigService;
import com.shazhu.nongjt.service.INjtUserRegisterService;
import com.shazhu.system.mapper.SysUserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.shazhu.nongjt.mapper.NjtUserPerfectMapper;
import com.shazhu.nongjt.domain.NjtUserPerfect;
import com.shazhu.nongjt.service.INjtUserPerfectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户完善信息Service业务层处理
 * 
 * @author shazhu
 * @date 2025-09-23
 */
@Service
public class NjtUserPerfectServiceImpl implements INjtUserPerfectService 
{


    @Autowired
    private NjtUserPerfectMapper njtUserPerfectMapper;

    /**
     * 查询用户完善信息
     * 
     * @param id 用户完善信息主键
     * @return 用户完善信息
     */
    @Override
    public NjtUserPerfect selectNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectById(id);
    }

    /**
     * 查询用户完善信息列表
     * 
     * @param njtUserPerfect 用户完善信息
     * @return 用户完善信息
     */
    @Override
    public List<NjtUserPerfect> selectNjtUserPerfectList(NjtUserPerfect njtUserPerfect)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectList(njtUserPerfect);
    }

    /**
     * 新增用户完善信息
     * 
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int insertNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setCreateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.insertNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 修改用户完善信息
     * 
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int updateNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setUpdateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 批量删除用户完善信息
     * 
     * @param ids 需要删除的用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectByIds(Long[] ids)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectByIds(ids);
    }

    /**
     * 删除用户完善信息信息
     * 
     * @param id 用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectById(id);
    }
    /**
     * 根据⽤户注册id查询会员完善信息
     *
     * @param userId
     * @return
     */
    @Override
    public NjtUserPerfect getByRegisterUserId(Long userId) {
        NjtUserPerfect query = new NjtUserPerfect();
        query.setUserId(userId);
        List<NjtUserPerfect> njtUserPerfects = selectNjtUserPerfectList(query)
                ;
        if (null != njtUserPerfects && !njtUserPerfects.isEmpty()) {
            return njtUserPerfects.get(0);
        }
        return null;
    }

    @Lazy
    @Autowired
    private INjtCommonService commonService;
    @Autowired
    private INjtUserRegisterService njtUserRegisterService;
    @Autowired
    private INjtUnitConfigService unitConfigService;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 完善信息
     *
     * @param userPerfect
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult perfectUser(NjtUserPerfect userPerfect) {

        // 获取当前登录用户信息

        NjtLoginUser loginUser = commonService.getLoginUser();

        // 获取用户注册信息
        NjtUserRegister userRegister = loginUser.getUserRegister();
        if (null == userRegister) {
            return AjaxResult.error("用户注册信息查询失败");
        }

        // 获取系统用户信息
        SysUser sysUser = loginUser.getSysUser().getUser();
        if (null == sysUser) {
            return AjaxResult.error("系统用户信息查询失败");
        }

        // 1.更新注册信息
        userRegister.setPerfectFlag(YesOrNoEnum.YES.getCode());
        // 更新完善时间
        userRegister.setPerfectTime(new Date());
        // 更新主体名称
        userRegister.setCompanyName(userPerfect.getCompanyName());
        userRegister.setUpdateTime(new Date());
        njtUserRegisterService.updateNjtUserRegister(userRegister);


        // 2.更新系统用户信息表
        sysUser.setNickName(userPerfect.getCompanyName());
        sysUser.setPhonenumber(userPerfect.getPhoneNumber());

        // 设置用户所属单位，通过完善信息提交的省市区查询单位信息表，拿到ID
        NjtUnitConfig unitConfig = new NjtUnitConfig();
        unitConfig.setProvince(userPerfect.getProvince());
        unitConfig.setCity(userPerfect.getCity());
        unitConfig.setArea(userPerfect.getArea());
        List<NjtUnitConfig> list = unitConfigService.selectNjtUnitConfigList(unitConfig);
        if (ObjectUtils.isNotEmpty(list)) {
            sysUser.setUnitId(list.get(0).getId());
        }
        sysUserMapper.updateUser(sysUser);


        // 3.新增或编辑完善信息
        userPerfect.setMobilePhone(userRegister.getMobilePhone());
        userPerfect.setUserId(userRegister.getId());
        userPerfect.setRegisterTime(userRegister.getRegisterTime());
        // 默认待审核状态
        userPerfect.setExamineStatus(NjtConstants.EXAMINING);

        if (null == userPerfect.getId()) {
            // 新增
            this.insertNjtUserPerfect(userPerfect);
        } else {
            // 编辑
            this.updateNjtUserPerfect(userPerfect);
        }

        return AjaxResult.success("操作成功");
    }
}
